package com.crazy.boot.util;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;


@ControllerAdvice
public class GlobalExceptionHandler {

    static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //算术异常
    @ExceptionHandler(value = ArithmeticException.class)
    @ResponseBody
    public ResultUtils arithmeticException(ArithmeticException e) {
        //e.printStackTrace();
        logger.info("MSG: "+ ExceptionCodeEnum.ARITHMETIC_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.ARITHMETIC_EXCEPTION.getCode(), ExceptionCodeEnum.ARITHMETIC_EXCEPTION.getMsg());
    }

    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultUtils nullPointerException(NullPointerException e) {
        logger.info("MSG: "+ ExceptionCodeEnum.NULL_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.NULL_EXCEPTION.getCode(), ExceptionCodeEnum.NULL_EXCEPTION.getMsg());
    }

    //SQL异常
    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public ResultUtils sQLException(SQLException e) {
        logger.info("MSG: "+ ExceptionCodeEnum.DATABASE_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.DATABASE_EXCEPTION.getCode(), ExceptionCodeEnum.DATABASE_EXCEPTION.getMsg());
    }

    //其他异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultUtils otherException(Exception e) {
        e.printStackTrace();
        logger.info("MSG: "+ ExceptionCodeEnum.OTHER_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.OTHER_EXCEPTION.getCode(), ExceptionCodeEnum.OTHER_EXCEPTION.getMsg());
    }

    @ExceptionHandler(value = UnknownAccountException.class)
    @ResponseBody
    public ResultUtils unknownAccountException(UnknownAccountException e) {
        logger.info("MSG: "+ ExceptionCodeEnum.SHIRO_USER_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.SHIRO_USER_EXCEPTION.getCode(), ExceptionCodeEnum.SHIRO_USER_EXCEPTION.getMsg());
    }

    @ExceptionHandler(value = IncorrectCredentialsException.class)
    @ResponseBody
    public ResultUtils incorrectCredentialsException(IncorrectCredentialsException e) {
        logger.info("MSG: "+ ExceptionCodeEnum.SHIRO_PASSWORD_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.SHIRO_PASSWORD_EXCEPTION.getCode(), ExceptionCodeEnum.SHIRO_PASSWORD_EXCEPTION.getMsg());
    }
    
    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseBody
    public ResultUtils unauthorizedException(UnauthorizedException e) {
        logger.info("MSG: "+ ExceptionCodeEnum.SHIRO_AUTHO_EXCEPTION.getMsg());
        return ResultUtils.toClient(ExceptionCodeEnum.SHIRO_AUTHO_EXCEPTION.getCode(), ExceptionCodeEnum.SHIRO_AUTHO_EXCEPTION.getMsg());
    }

}
